/**
 * Created by gaoyj on 2018/6/28.
 */
window.onload = function (ev) {
    topbar.hide();
};
//销卡模板
var str;
$.post("productPrice/findModelPrice", null, function (data) {
    var group_obj = data;
    for (var i = 0; i < group_obj.length; i++) {
        str += '<option value="' + group_obj[i].id + '">' + group_obj[i].modelName + '</option>'
    }
    $("#supplyModel").append(str);
});
initProvinces();
$("#quickSetting").validate({
    rules: {
        minPrice: {
            required: true,
            range: [0.5, 1.0]
        }, maxPrice: {
            required: true,
            range: [0.5, 1.0]
        }

    }
});

function initProvinces() {
    var str = '';
    for (var i = 0; i < zone.length; i++) {
        str += '<label class="icheck" style="margin-right:10px;">' +
            '<input name="areas" type="checkbox" value="' + zone[i] + '">' + zone[i] + '</label>';
    }
    $("#areaSet").html(str);
}

function reloadDiscount(obj) {
    if ($(obj).val() == '1' || $(obj).val() == '0') {
        $("#minPrice").removeAttr("readonly", "readonly")
        $("#maxPrice").removeAttr("readonly", "readonly")
    } else {
        $("#minPrice").attr("readonly", "readonly");
        $("#maxPrice").attr("readonly", "readonly");
    }
}

$("#formSubmit").click(function () {
    if ($('input[name=productTypes]:checked').length < 1) {
        layer.msg("请选择运营商");
        return;
    }
    if ($('input[name=faces]:checked').length < 1) {
        layer.msg("请选择面值");
        return;
    }
    if ($('input[name=areas]:checked').length < 1) {
        layer.msg("请选择销卡地区");
        return;
    }
    if ($("#method").val() ==0||$("#method").val() ==1) {
        var v = $("#quickSetting").valid();
        if (!v) {
            return;
        }
        if($("#minPrice").val()>$("#maxPrice").val()){
            layer.msg("最大折扣不可小于最低折扣");
            return;
        }
    }
    $.confirm({
        title: '确认!',
        content: '确定提交吗？',
        confirm: function () {
            submitForm();
        }
    })
});


function submitForm() {
    $.post("/productPrice/setUpModelPrice", $("#quickSetting").serialize(), function (data) {
        layer.msg(data.message);
    }).error(function (e) {
        layer.msg(e);
    });
}